package com.libui.toast

import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.os.CountDownTimer
import com.libbase.utils.FileUtils
import com.libui.LibUI
import com.libui.R
import com.libui.webview.WebViewEx

typealias ToastCompletion = () -> Unit

class Toast private constructor(context: Context, builder: Builder) : Dialog(context) {
    companion object {
        var currentToast: Toast? = null

        fun closeToast() {
            currentToast?.dismiss()
        }
    }

    interface Builder {
        val message: String

        val baseUrl: String?

        val htmlStr: String

        var interactiveHide: Boolean

        var duration: Long?

        var completion: ToastCompletion?

        fun build(context: Context): Toast
    }

    class ToastBuilder(override val message: String) : Builder {
        override lateinit var htmlStr: String

        override var baseUrl: String? = null

        override var interactiveHide: Boolean = false

        override var duration: Long? = null

        override var completion: ToastCompletion? = null

        override fun build(context: Context): Toast {
            val htmlPath = LibUI.instance.dataSource?.htmlPath ?: return Toast(context, this)

            baseUrl = htmlPath

            var htmlStr = FileUtils.instance.readAssetFile("${htmlPath}/toast.html", context)
            if (htmlStr != null) {
                htmlStr = htmlStr.replace("{{ message }}", message)
                this.htmlStr = htmlStr
            }

            return Toast(context, this)
        }

        fun setInteractiveHide(interactiveHide: Boolean): ToastBuilder {
            this.interactiveHide = interactiveHide
            return this
        }

        fun setCompletion(completion: ToastCompletion?): ToastBuilder {
            this.completion = completion
            return this
        }

        fun setDuration(duration: Long): ToastBuilder {
            this.duration = duration
            return this
        }
    }

    class LoadingBuilder(override val message: String) : Builder {
        override lateinit var htmlStr: String

        override var baseUrl: String? = null

        override var interactiveHide: Boolean = false

        override var duration: Long? = null

        override var completion: ToastCompletion? = null

        override fun build(context: Context): Toast {
            val htmlPath = LibUI.instance.dataSource?.htmlPath ?: return Toast(context, this)

            baseUrl = htmlPath

            var htmlStr = FileUtils.instance.readAssetFile("${htmlPath}/loading.html", context)
            if (htmlStr != null) {
                htmlStr = htmlStr.replace("{{ message }}", message)
                this.htmlStr = htmlStr
            }

            return Toast(context, this)
        }

        fun setInteractiveHide(interactiveHide: Boolean): LoadingBuilder {
            this.interactiveHide = interactiveHide
            return this
        }

        fun setDuration(duration: Long): LoadingBuilder {
            this.duration = duration
            return this
        }

        fun setCompletion(completion: ToastCompletion?): LoadingBuilder {
            this.completion = completion
            return this
        }
    }

    private val interactiveHide: Boolean = builder.interactiveHide

    private val duration: Long? = builder.duration

    private val htmlStr: String = builder.htmlStr

    private val baseUrl: String? = builder.baseUrl

    private val completion: ToastCompletion? = builder.completion

    private var timer: CountDownTimer? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContentView(R.layout.toast)
        window?.setBackgroundDrawableResource(android.R.color.transparent)

        setCanceledOnTouchOutside(interactiveHide)
        setCancelable(interactiveHide)

        setOnShowListener {
            currentToast = this
        }

        setOnDismissListener {
            currentToast = null
            completion?.let { it() }
        }

        val webView: WebViewEx = findViewById(R.id.web_view)
        webView.setBackgroundColor(0)
        webView.loadDataWithBaseURL(
            if (baseUrl != null) "file:///android_asset/${baseUrl}/" else null,
            htmlStr,
            "text/html",
            "utf-8",
            null
        )
    }

    override fun onStart() {
        super.onStart()

        duration?.let {
            timer = object : CountDownTimer(it, it) {
                override fun onTick(millisUntilFinished: Long) {
                }

                override fun onFinish() {
                    dismiss()
                }
            }.start()
        }
    }

    override fun dismiss() {
        super.dismiss()

        timer?.cancel()
    }
}